﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HandRise.MBHOP.Controls;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmTaskDetail : FrmBase
    {
        private Plan plan;
        private PlanService planService;

        public FrmTaskDetail()
        {
            InitializeComponent();
            ttlbrhead.Text = "计划详情";
            ilstdown.Images.Add(ResourceImage.s_定位02);
            ilstdown.Images.Add(ResourceImage.s_执行02);
            ilstdown.Images.Add(ResourceImage.s_完成02);
            ilstdown.Images.Add(ResourceImage.s_返回02);
            ilstup.Images.Add(ResourceImage.s_定位01);
            ilstup.Images.Add(ResourceImage.s_执行01);
            ilstup.Images.Add(ResourceImage.s_完成01);
            ilstup.Images.Add(ResourceImage.s_返回01);

        }

        private void frenwumx_Load(object sender, EventArgs e)
        {
            
            ccPlanDetail.AddMember("StartDateTime", "开始时间");
            ccPlanDetail.AddMember("EndDateTime", "结束时间");
            ccPlanDetail.AddMember("Content", "巡视内容");
            ccPlanDetail.AddMember("Range", "巡视范围");
            ccPlanDetail.AddMember("State", "状态");

            planService = new PlanService();

            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            plan = (Plan)Parameter[0];
            ccPlanDetail.DataSource = plan;
            ccPlanDetail.DataBind();

            tbPlanTitle.Text = plan.Title;

        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            plan = (Plan)Caches[0];
            ccPlanDetail.DataSource = plan;
            ccPlanDetail.DataBind();

            tbPlanTitle.Text = plan.Title;
        }

        /// <summary>
        /// 完成计划
        /// </summary>
        private void CompletePlan()
        {
            if (plan.State == "已完成")
            {
                mssdlgComfirm.ShowDialog(this, "当前计划已经完成", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (mssdlgexec.ShowDialog(this, "确认完成该计划？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
            {
                plan = planService.CompletePlan(plan.PlanID);
                ccPlanDetail.DataSource = plan;
                ccPlanDetail.DataBind();
            }
        }

        /// <summary>
        /// 执行计划
        /// </summary>
        private void ExcutePlan()
        {
            if (plan.State == "已完成")
            {
                mssdlgComfirm.ShowDialog(this, "当前计划已经完成", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (plan.State == "已下达")
            {
                plan = planService.ExcutePlan(plan.PlanID);
                ccPlanDetail.DataSource = plan;
                ccPlanDetail.DataBind();
            }

            if(plan.State == "执行中")
                FormManage.GotoNext("FrmPatrolType", new object[] { plan }, new object[] { plan }, false);
        }



        private void btnPlanExecuteDetail_Click(object sender, EventArgs e)
        {
            FormManage.GotoNext("FrmTaskExecDetail", new object[] { plan }, new object[] { plan }, false);
        }

        private void btnPrior_Click(object sender, EventArgs e)
        {
            ccPlanDetail.PreviousPage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            ccPlanDetail.NextPage();
        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:
                    FormManage.GotoNext("FrmTaskPos", new object[] { plan }, new object[] { plan }, false);
                    break;
                case 1:
                    ExcutePlan();
                    break;
                case 2:
                    CompletePlan();
                    break;
                case 3:
                    FormManage.GotoNext("FrmTask", null, null, true);
                    break;
            }
        }


    
    }
}